package com.sky.controller;


import cn.hutool.core.lang.UUID;
import com.sky.constant.MessageConstant;
import com.sky.entity.result.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

@RestController
@RequestMapping
@Slf4j
@Tag(name = "文件上传")
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        // 把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        // 保证文件的名唯一
        String fileName = null;
        if (originalFilename != null) {
            fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        Path path = Path.of("e:\\upload");
        file.transferTo(new File(path + "\\" + fileName));
        return Result.success(MessageConstant.UPLOAD_SUCCESS);
    }

}
